package com.movecar.ui;

import java.util.ArrayList;
import java.util.List;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.movecar.R;
import com.movecar.model.GroupBean;
import com.movecar.utils.Alert;
import com.movecar.utils.AppConfig;
import com.movecar.utils.DataStore;
import com.movecar.utils.HttpPostRequestAsyncTask;
import com.movecar.utils.RequestResultCallback;
import com.movecar.widget.CustomProgressDialog;

/**
 * 主页-设置备注名
 * 
 * @author Zhao.JQ
 * 
 */

public class AddFriendRemarkActivity extends Activity implements android.view.View.OnClickListener {
	private EditText remark_name;
	private InputMethodManager imm;
	private Button send, back;
	private CustomProgressDialog progressDialog = null;
	private String friendId;
	private String oldRemark;

	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.add_friend_remark_name);
		imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
		Intent intent = getIntent();
		friendId = intent.getStringExtra("friendId");
		oldRemark = intent.getStringExtra("oldRemark");
		initView();
	}

	private void initView() {
		back = (Button) findViewById(R.id.btn_left);
		back.setVisibility(View.VISIBLE);
		back.setOnClickListener(this);
		send = (Button) findViewById(R.id.send);
		send.setOnClickListener(this);
		TextView title = (TextView) findViewById(R.id.title);
		title.setText("好友备注");
		remark_name = (EditText) findViewById(R.id.remark_name);
		remark_name.setText(oldRemark);
		imm.showSoftInput(remark_name, InputMethodManager.RESULT_SHOWN);
		imm.toggleSoftInput(InputMethodManager.SHOW_FORCED, InputMethodManager.HIDE_IMPLICIT_ONLY);
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.btn_left:
			imm.hideSoftInputFromWindow(remark_name.getWindowToken(), 0);
			finish();
			break;
		case R.id.send:

			startProgressDialog();
			updateRemark();

			break;

		default:
			break;
		}
	}

	/**
	 * 申请添加好友
	 */
	@SuppressWarnings("unchecked")
	private void updateRemark() {

		List<NameValuePair> params = new ArrayList<NameValuePair>(); //
		// 使用NameValuePair来保存要传递的Post参数
		params.add(new BasicNameValuePair("cmd", "updateRemarksNew1")); // 添加要传递的参数
		params.add(new BasicNameValuePair("userId", DataStore.getInstance().getUserId()));
		params.add(new BasicNameValuePair("friendId", friendId));
		params.add(new BasicNameValuePair("content", remark_name.getText().toString().trim()));
		new HttpPostRequestAsyncTask(new RequestResultCallback() {

			@Override
			public void onSuccess(Object object) {

				String result = (String) object;
				if ("0".equals(result)) {
					stopProgressDialog();
					Toast.makeText(getApplicationContext(), "操作成功", Toast.LENGTH_SHORT).show();
					setResult(RESULT_OK);
					imm.hideSoftInputFromWindow(remark_name.getWindowToken(), 0);
					finish();
				} else {
					stopProgressDialog();
					Toast.makeText(getApplicationContext(), result, Toast.LENGTH_SHORT).show();
				}

			}

			@Override
			public void onFail(String exception) {

				Alert.error(AddFriendRemarkActivity.this,"与服务器连接失败，请检查网络");
			}
		}, GroupBean.class, AppConfig.USER).execute(params);
	}

	private void startProgressDialog() {
		if (progressDialog == null) {
			progressDialog = CustomProgressDialog.createDialog(this);
			progressDialog.setMessage("正在加载...");
		}

		progressDialog.show();
	}

	private void stopProgressDialog() {
		if (progressDialog != null) {
			progressDialog.dismiss();
			progressDialog = null;
		}
	}

}
